home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / djgpp / src / gas-211 / gas / input-fi.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-30  |  5.4 KB  |  243 lines

  1. /* input_file.c - Deal with Input Files -
  2.    Copyright (C) 1987, 1990, 1991, 1992 Free Software Foundation, Inc.
  3.  
  4.    This file is part of GAS, the GNU Assembler.
  5.  
  6.    GAS is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2, or (at your option)
  9.    any later version.
  10.  
  11.    GAS is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with GAS; see the file COPYING.  If not, write to
  18.    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /*
  21.  * Confines all details of reading source bytes to this module.
  22.  * All O/S specific crocks should live here.
  23.  * What we lose in "efficiency" we gain in modularity.
  24.  * Note we don't need to #include the "as.h" file. No common coupling!
  25.  */
  26.  
  27. #include <stdio.h>
  28. #include <assert.h>
  29. #include <string.h>
  30.  
  31. #include "as.h"
  32. #include "input-file.h"
  33.  
  34. /* This variable is non-zero if the file currently being read should be
  35.    preprocessed by app.  It is zero if the file can be read straight in.
  36.    */
  37. int preprocess = 0;
  38.  
  39. /*
  40.  * This code opens a file, then delivers BUFFER_SIZE character
  41.  * chunks of the file on demand.
  42.  * BUFFER_SIZE is supposed to be a number chosen for speed.
  43.  * The caller only asks once what BUFFER_SIZE is, and asks before
  44.  * the nature of the input files (if any) is known.
  45.  */
  46.  
  47. #define BUFFER_SIZE (32 * 1024)
  48.  
  49. /*
  50.  * We use static data: the data area is not sharable.
  51.  */
  52.  
  53. FILE *f_in;
  54. /* static JF remove static so app.c can use file_name */
  55. char *file_name;
  56.  
  57. /* Struct for saving the state of this module for file includes.  */
  58. struct saved_file
  59.   {
  60.     FILE *f_in;
  61.     char *file_name;
  62.     int preprocess;
  63.     char *app_save;
  64.   };
  65.  
  66. /* These hooks accomodate most operating systems. */
  67.  
  68. void 
  69. input_file_begin ()
  70. {
  71.   f_in = (FILE *) 0;
  72. }
  73.  
  74. void 
  75. input_file_end ()
  76. {
  77. }
  78.  
  79. /* Return BUFFER_SIZE. */
  80. int 
  81. input_file_buffer_size ()
  82. {
  83.   return (BUFFER_SIZE);
  84. }
  85.  
  86. int 
  87. input_file_is_open ()
  88. {
  89.   return f_in != (FILE *) 0;
  90. }
  91.  
  92. /* Push the state of our input, returning a pointer to saved info that
  93.    can be restored with input_file_pop ().  */
  94. char *
  95. input_file_push ()
  96. {
  97.   register struct saved_file *saved;
  98.  
  99.   saved = (struct saved_file *) xmalloc (sizeof *saved);
  100.  
  101.   saved->f_in = f_in;
  102.   saved->file_name = file_name;
  103.   saved->preprocess = preprocess;
  104.   if (preprocess)
  105.     saved->app_save = app_push ();
  106.  
  107.   input_file_begin ();        /* Initialize for new file */
  108.  
  109.   return (char *) saved;
  110. }
  111.  
  112. void
  113. input_file_pop (arg)
  114.      char *arg;
  115. {
  116.   register struct saved_file *saved = (struct saved_file *) arg;
  117.  
  118.   input_file_end ();        /* Close out old file */
  119.  
  120.   f_in = saved->f_in;
  121.   file_name = saved->file_name;
  122.   preprocess = saved->preprocess;
  123.   if (preprocess)
  124.     app_pop (saved->app_save);
  125.  
  126.   free (arg);
  127. }
  128.  
  129. void
  130. input_file_open (filename, pre)
  131.      char *filename;        /* "" means use stdin. Must not be 0. */
  132.      int pre;
  133. {
  134.   int c;
  135.   char buf[80];
  136.  
  137.   preprocess = pre;
  138.  
  139.   assert (filename != 0);    /* Filename may not be NULL. */
  140.   if (filename[0])
  141.     {                /* We have a file name. Suck it and see. */
  142.       f_in = fopen (filename, "r");
  143.       file_name = filename;
  144.     }
  145.   else
  146.     {                /* use stdin for the input file. */
  147.       f_in = stdin;
  148.       file_name = "{standard input}";    /* For error messages. */
  149.     }
  150.   if (f_in == (FILE *) 0)
  151.     {
  152.       as_perror ("Can't open %s for reading", file_name);
  153.       return;
  154.     }
  155.  
  156.   c = getc (f_in);
  157.   if (c == '#')
  158.     {                /* Begins with comment, may not want to preprocess */
  159.       c = getc (f_in);
  160.       if (c == 'N')
  161.     {
  162.       fgets (buf, 80, f_in);
  163.       if (!strcmp (buf, "O_APP\n"))
  164.         preprocess = 0;
  165.       if (!strchr (buf, '\n'))
  166.         ungetc ('#', f_in);    /* It was longer */
  167.       else
  168.         ungetc ('\n', f_in);
  169.     }
  170.       else if (c == '\n')
  171.     ungetc ('\n', f_in);
  172.       else
  173.     ungetc ('#', f_in);
  174.     }
  175.   else
  176.     ungetc (c, f_in);
  177. }
  178.  
  179. /* Close input file.  */
  180. void 
  181. input_file_close ()
  182. {
  183.   if (f_in != NULL)
  184.     {
  185.       fclose (f_in);
  186.     }                /* don't close a null file pointer */
  187.   f_in = 0;
  188. }                /* input_file_close() */
  189.  
  190. char *
  191. input_file_give_next_buffer (where)
  192.      char *where;        /* Where to place 1st character of new buffer. */
  193. {
  194.   char *return_value;        /* -> Last char of what we read, + 1. */
  195.   register int size;
  196.  
  197.   if (f_in == (FILE *) 0)
  198.     return 0;
  199.   /*
  200.    * fflush (stdin); could be done here if you want to synchronise
  201.    * stdin and stdout, for the case where our input file is stdin.
  202.    * Since the assembler shouldn't do any output to stdout, we
  203.    * don't bother to synch output and input.
  204.    */
  205.   if (preprocess)
  206.     {
  207.       char *p;
  208.       int n;
  209.       int ch;
  210.       extern FILE *scrub_file;
  211.  
  212.       scrub_file = f_in;
  213.       for (p = where, n = BUFFER_SIZE; n; --n)
  214.     {
  215.  
  216.       ch = do_scrub_next_char (scrub_from_file, scrub_to_file);
  217.       if (ch == EOF)
  218.         break;
  219.       *p++ = ch;
  220.     }
  221.       size = BUFFER_SIZE - n;
  222.     }
  223.   else
  224.     size = fread (where, sizeof (char), BUFFER_SIZE, f_in);
  225.   if (size < 0)
  226.     {
  227.       as_perror ("Can't read from %s", file_name);
  228.       size = 0;
  229.     }
  230.   if (size)
  231.     return_value = where + size;
  232.   else
  233.     {
  234.       if (fclose (f_in))
  235.     as_perror ("Can't close %s", file_name);
  236.       f_in = (FILE *) 0;
  237.       return_value = 0;
  238.     }
  239.   return (return_value);
  240. }
  241.  
  242. /* end of input-file.c */
  243.